package com.songtao.rabbitmq.workfair;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.songtao.rabbitmq.config.RabbitConfig;
import com.songtao.rabbitmq.util.RabbitConnectionUtils;

public class Send {
    
    
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        
        Connection connection = RabbitConnectionUtils.getConnection();
        
        Channel channel = connection.createChannel();
        
        //声明队列
        channel.queueDeclare(RabbitConfig.QUEUE_SECOND, false, false, false, null);
        //保证只分发一次
        int prefetchCount = 1;
        channel.basicQos(prefetchCount);
        //消息
        String msg = "";
        for (int i = 0; i < 50; i++) {
            msg = "hello rabbitmq "+i;
            channel.basicPublish("", RabbitConfig.QUEUE_SECOND, null, msg.getBytes());
            System.out.println("发送  "+msg);
            Thread.sleep(1000);
        }
        
        channel.close();
        connection.close();
        
    }
}
